package com.zeek.car90s;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.avast.android.dialogs.core.BaseDialogFragment;
import com.avast.android.dialogs.fragment.SimpleDialogFragment;
import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.game.UMGameAgent;
import com.zeek.car90s.util.DialogHelp;
import com.zeek.car90s.util.MediaPlayerUtils;
import com.zeek.car90s.util.Tools;

public class UseCoinsFragment extends SimpleDialogFragment implements View.OnClickListener {
    public static String TAG = "use_coins";
    private SharedPreferences sp;
    private Button btnOK, btnEarnCoins, btnTotalCoins, btnSolve,coinSolve,
            btnRandomLetter,coinRandomLetter,btnRemoveOneLetter,coinRemoveOneLetter,btnRemoveExtraLetters,coinRemoveExtraLetters;

    public static void show(FragmentActivity activity) {
        new UseCoinsFragment().show(activity.getSupportFragmentManager(), TAG);
    }

    @Override
    public int getTheme() {
        return R.style.JayneHatDialogTheme;
    }

    @Override
    public BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_use_coins, null);
        builder.setView(view);

        sp = getActivity().getSharedPreferences(Constants.SHARED_PREFERENCE_PREFIX, Context.MODE_PRIVATE);

        btnTotalCoins = (Button) view.findViewById(R.id.btn_total_coin);
        btnTotalCoins.setText(Constants.TOTAL_COIN + "");

        //随机提示一个字母
        btnRandomLetter= (Button) view.findViewById(R.id.btn_random_letter);
        btnRandomLetter.setOnClickListener(this);

        coinRandomLetter= (Button) view.findViewById(R.id.coin_random_letter);
        coinRandomLetter.setOnClickListener(this);
        coinRandomLetter.setText(-Constants.COINS_COST_RANDOM_LETTER+"");

        //随机去除一个多余字母
        btnRemoveOneLetter= (Button) view.findViewById(R.id.btn_remove_one_letter);
        btnRemoveOneLetter.setOnClickListener(this);

        coinRemoveOneLetter= (Button) view.findViewById(R.id.coin_remove_one_letter);
        coinRemoveOneLetter.setOnClickListener(this);
        coinRemoveOneLetter.setText(-Constants.COINS_COST_REMOVE_ONE_LETTER+"");

        //去除所有多余的字母
        btnRemoveExtraLetters= (Button) view.findViewById(R.id.btn_remove_extra_letters);
        btnRemoveExtraLetters.setOnClickListener(this);

        coinRemoveExtraLetters= (Button) view.findViewById(R.id.coin_remove_extra_letters);
        coinRemoveExtraLetters.setOnClickListener(this);
        coinRemoveExtraLetters.setText(-Constants.COINS_COST_REMOVE_EXTRA_LETTERS+"");

        //直接解决问题
        btnSolve= (Button) view.findViewById(R.id.btn_solve);
        btnSolve.setOnClickListener(this);

        coinSolve= (Button) view.findViewById(R.id.coin_solve);
        coinSolve.setOnClickListener(this);
        coinSolve.setText(-Constants.COINS_COST_SOLVE+"");

        btnOK = (Button) view.findViewById(R.id.btn_OK);
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayerUtils.playNotification(1);
                dismiss();
            }
        });

        btnEarnCoins = (Button) view.findViewById(R.id.btn_earn_coins);
        btnEarnCoins.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayerUtils.playNotification(1);
                startActivity(new Intent(getActivity(), EarnCoinsActivity.class));
                MobclickAgent.onEvent(getContext(), "earn_coin_from_total_ok");
            }
        });

        return builder;
    }

    @Override
    public void onClick(View v) {
        int selectedId=v.getId();
        switch (selectedId){
            case R.id.btn_random_letter:
            case R.id.coin_random_letter:
                costRandomLetterCoins();
                break;
            case R.id.btn_remove_one_letter:
            case R.id.coin_remove_one_letter:
                costRemoveOneLetterCoins();
                break;
            case R.id.btn_remove_extra_letters:
            case R.id.coin_remove_extra_letters:
                costRemoveExtraLettersCoins();
                break;
            case R.id.btn_solve:
            case R.id.coin_solve:
                costSolveCoins();
                break;
        }
    }

    //随机提示一个字母
    private void costRandomLetterCoins(){
        if(!isCoinsEnough(Constants.COINS_COST_RANDOM_LETTER)){
            return;
        }

        ((GameActivity) getActivity()).randomLetter();
        UMGameAgent.buy("random letter", 1, Constants.COINS_COST_RANDOM_LETTER);
        MobclickAgent.onEvent(getActivity(), "cost_coins_random_letter");
    }

    //移除一个多余字母
    private void costRemoveOneLetterCoins(){
        if(!isCoinsEnough(Constants.COINS_COST_REMOVE_ONE_LETTER)){
            return;
        }

        ((GameActivity) getActivity()).removeOneLetter();
        UMGameAgent.buy("remove one letter", 1, Constants.COINS_COST_REMOVE_ONE_LETTER);
        MobclickAgent.onEvent(getActivity(), "cost_coins_remove_one");
    }

    //移除全部的多余字母
    private void costRemoveExtraLettersCoins(){
        if(!isCoinsEnough(Constants.COINS_COST_REMOVE_EXTRA_LETTERS)){
            return;
        }

        ((GameActivity) getActivity()).removeExtraLetters();
        UMGameAgent.buy("remove extra letters", 1, Constants.COINS_COST_REMOVE_EXTRA_LETTERS);
        MobclickAgent.onEvent(getActivity(), "cost_coins_remove_extra");
    }

    //解决问题消耗金币
    private void costSolveCoins() {
        if(!isCoinsEnough(Constants.COINS_COST_SOLVE)){
            return;
        }

        ((GameActivity) getActivity()).solveQuestion(Constants.COINS_COST_SOLVE);
        UMGameAgent.buy("solve", 1, Constants.COINS_COST_SOLVE);
        MobclickAgent.onEvent(getActivity(), "cost_coins_solve");
    }

    private void refreshCoin() {
        btnTotalCoins.setText(Constants.TOTAL_COIN + "");

        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(Constants.COIN_PREFIX, Constants.TOTAL_COIN);
        editor.commit();

        if (Constants.TOTAL_COIN > 9999) {
            btnTotalCoins.getLayoutParams().width = (int) Tools.convertDpToPx(66, getActivity());
        } else if (Constants.TOTAL_COIN > 999) {
            btnTotalCoins.getLayoutParams().width = (int) Tools.convertDpToPx(58, getActivity());
        }
    }

    private boolean isCoinsEnough(int coinCost){
        if(Constants.TOTAL_COIN<coinCost){
            DialogHelp.getMessageDialog(getActivity(), getActivity().getString(R.string.coins_not_enough_hint)).show();
            return false;
        }else {
            dismiss();
            return true;
        }
    }
}
